package com.qdsg.ysg.doctor.ui.dialog;

import android.app.Dialog;
import android.graphics.drawable.ColorDrawable;
import android.location.Address;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.qdsg.ysg.doctor.R;
import com.qdsg.ysg.doctor.base.BaseDialogFragment;
import com.qdsg.ysg.doctor.ui.adapter.PoliceAddressAdapter;
import com.qdsg.ysg.doctor.util.ToastUtil;
import com.qdsg.ysg.doctor.util.UtilString;
import com.rest.business.RestProxy;
import com.rest.response.DocOrderBaseVO;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import jz.joyoung.robot.util.UtilList;

/**
 * Description
 * Created by lbj on 2022/4/8 14:38
 */

public class PoliceCallDialog extends BaseDialogFragment {


    private TextView tvNoPolice;
    private Address address;
    private String addressLocation;

    public static PoliceCallDialog newInstance(Address address) {
        Bundle args = new Bundle();
        args.putParcelable( "address", address );
        PoliceCallDialog fragment = new PoliceCallDialog();
        fragment.setArguments( args );
        return fragment;
    }

    private Dialog mDialog;
    private OnItemClickListener mListener;
    private RecyclerView rvPoliceAddress;
    private PoliceAddressAdapter adapter;

    private List<DocOrderBaseVO.DocOrderBaseDTO> mList = new ArrayList<>();

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate( R.layout.dialog_police_call, container );
        rvPoliceAddress = view.findViewById( R.id.rv_police_address );
        tvNoPolice = view.findViewById( R.id.tv_no_police );
        TextView tvSendAddress = view.findViewById( R.id.tv_send_address );
        TextView tvCancel = view.findViewById( R.id.tv_cancel );
        LinearLayoutManager manager = new LinearLayoutManager( mContext );
        manager.setOrientation( LinearLayoutManager.VERTICAL );
        rvPoliceAddress.setLayoutManager( manager );
        adapter = new PoliceAddressAdapter( mContext, mList );
        rvPoliceAddress.setAdapter( adapter );
        if (getArguments() != null) {
            address = getArguments().getParcelable( "address" );
            assert address != null;
            addressLocation = address.getAddressLine( 0 );
            currentPatientList();
        }


        tvSendAddress.setOnClickListener( v -> {
            mListener.onConfirm( addressLocation, "", "" );
            dismiss();
//            ChooseAddressDialog mChooseAddressDialog = ChooseAddressDialog.newInstance( address );
//            mChooseAddressDialog.show( getChildFragmentManager(), "ChooseAddressDialog" );
//            mChooseAddressDialog.setOnItemClickListener( (address) -> {
//                addressLocation = address;
//
//
//            } );
        } );
        adapter.setOnItemClickLitener( (view1, position) -> {
//            if (UtilString.isEmpty( addressLocation )){
//                ToastUtil.showToastCenter( mContext,"请发送当前位置信息" );
//                return;
//            }
            for (int i = 0; i < mList.size(); i++) {
                mList.get( i ).isSelect = false;
            }
            mList.get( position ).isSelect = true;
            adapter.notifyDataSetChanged();

            mListener.onConfirm( addressLocation, mList.get( position ).adress, mList.get( position ).name );
            dismiss();
//            adapter.setPosition( position );
//            adapter.notifyDataSetChanged();
//            schTime = list.get( position ).schTime;
//            scheduleId = list.get( position ).scheduleId;
//            price = list.get( position ).price;
//            Log.d( "price", price );
        } );
        tvCancel.setOnClickListener( v -> {
            if (mListener != null) {
                dismiss();
            }
        } );
        return view;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate( savedInstanceState );
    }

    @Override
    @NonNull
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        mDialog = super.onCreateDialog( savedInstanceState );
        mDialog.setCancelable( false );
        Window window = mDialog.getWindow();
        if (window != null) {
            window.setGravity( Gravity.BOTTOM );
//            window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
//            window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        }
        return mDialog;
    }


    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        Objects.requireNonNull( Objects.requireNonNull( getDialog() ).getWindow() ).requestFeature( Window.FEATURE_NO_TITLE );
        super.onActivityCreated( savedInstanceState );
        Objects.requireNonNull( getDialog().getWindow() ).setBackgroundDrawable( new ColorDrawable( 0x00000000 ) );
        getDialog().getWindow().setLayout( WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT );
    }

    public OnItemClickListener getmListener() {
        return mListener;
    }

    public void setmListener(OnItemClickListener mListener) {
        this.mListener = mListener;
    }


    public interface OnItemClickListener {

        void onConfirm(String address, String appointmentAddress, String name);
    }

    public boolean isShowing() {
        return mDialog != null && mDialog.isShowing();
    }


    /**
     * 当前患者列表
     */
    private void currentPatientList() {
        RestProxy.getInstance().currentPatientList( new Observer<DocOrderBaseVO>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(DocOrderBaseVO docOrderBaseVO) {
                try {
                    if (!UtilList.isEmpty( docOrderBaseVO.data )) {
                        tvNoPolice.setVisibility( View.GONE );
                        rvPoliceAddress.setVisibility( View.VISIBLE );
                        mList.clear();
                        mList.addAll( docOrderBaseVO.data );
//                        mList.get( 0 ).isSelect = true;
                        adapter.notifyDataSetChanged();

                    } else {
                        tvNoPolice.setVisibility( View.VISIBLE );
                        rvPoliceAddress.setVisibility( View.GONE );
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onError(Throwable e) {
                ToastUtil.onError( mContext, e );
            }

            @Override
            public void onComplete() {

            }
        } );

    }

}
